package com.siwei.mes.controller;

import com.siwei.mes.common.annotation.AnonymousAccess;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.util.JsonUtils;
import com.siwei.mes.entity.erp.Itemorder;
import com.siwei.mes.mapper.erp.ItemorderMapper;
import com.siwei.mes.mapper.experiment.ExperimentItemorderMapper;
import com.siwei.mes.service.erp.WblistService;
import com.siwei.mes.sync.config.SyncProcessorService;
import com.siwei.mes.sync.erp.entity.ItemorderFinish;
import com.siwei.mes.sync.erp.entity.SyncErpWaybill;
import com.siwei.mes.sync.erp.enums.ErpOrgState;
import com.siwei.mes.sync.erp.service.SyncErpWaybillService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 补偿机制接口
 *
 * @Description: 补偿机制接口
 * @Version: 1.0
 * @author: mlchen
 * @date: 2025/1/9 22:13
 */
@Slf4j
@Api(value = "补偿机制接口")
@RestController
@RequestMapping("compensate")
public class CompensateController {
    @Resource
    private ItemorderMapper itemorderMapper;
    @Resource
    private ExperimentItemorderMapper experimentItemorderMapper;
    @Resource
    private SyncProcessorService syncProcessorService;
    @Resource
    private WblistService wblistService;
    @Resource
    private SyncErpWaybillService syncErpWaybillService;


    /**
     * 补偿混凝土
     *
     * @param time 某个时间之后
     * @return
     */
    @AnonymousAccess
    @GetMapping("genConcrete")
    public Json testGenConcrete(@RequestParam("time") String time) {
        List<Itemorder> itemorderList = itemorderMapper.getCompensateInfo(time);
        log.info("混凝土补偿开始，时间：【{}】，数量：【{}】", time, itemorderList.size());
        for (Itemorder itemorder : itemorderList) {
            Integer itid = itemorder.getItid();
            List<Long> isExist = experimentItemorderMapper.selectByItemorderId(itid);
            if (!isExist.isEmpty()) {
                log.info("该小票已经生成试验委托不补偿，小票号：【{}】，任务单号：【{}】", itemorder.getItid(), itemorder.getFrwdh());
                continue;
            }
            ItemorderFinish itemorderFinish = new ItemorderFinish();
            itemorderFinish.setITID(itid);
            itemorderFinish.setFRwdh(itemorder.getFrwdh());
            itemorderFinish.setFinishCarNumber(itemorder.getFinishcarnumber());
            log.info("开始补偿小票：【{}】", JsonUtils.getJson(itemorderFinish));
            syncProcessorService.submitRequest(itemorderFinish);
        }
        return Json.succ();
    }

    /**
     * 补偿原材料
     *
     * @param time 某个时间之后的数据
     * @return
     */
    @AnonymousAccess
    @GetMapping("genMaterials")
    public Json genExperimentByWaybill(@RequestParam("time") String time,
                                       @RequestParam(required = false, name = "yclName") String yclName,
                                       @RequestParam(required = false, name = "pzgg") String pzgg) {

        List<SyncErpWaybill> syncErpWaybillList = wblistService.getCompensateInfo(time, yclName, pzgg);
        log.info("原材料补偿开始，时间：【{}】，符合条件的数量：【{}】", time, syncErpWaybillList.size());

        for (SyncErpWaybill erpWaybill : syncErpWaybillList) {

            String orgState = erpWaybill.getOrgState();
            if (StringUtils.isBlank(orgState)) {
                log.info("该运单状态为空，直接默认已卸货状态，运单号：【{}】", erpWaybill.getWLID());
                erpWaybill.setOrgState(ErpOrgState.YXH);
            }

            log.info("开始补偿运单：【{}】", JsonUtils.getJson(erpWaybill));
            syncErpWaybillService.saveOrUpdate(erpWaybill);
        }
        return Json.succ();
    }

    /**
     * 补偿原材料 根据WLID查询 支持多个
     *
     * @param wlIdList 多个运单id 用逗号隔开
     * @return
     */
    @AnonymousAccess
    @GetMapping("genMaterialsByIdList")
    public Json genMaterialsByIdList(@RequestParam("wlIdList") String wlIdList) {
        List<String> idList = Arrays.asList(wlIdList.split(","));
        List<SyncErpWaybill> syncErpWaybillList = wblistService.getCompensateInfoByWlIdList(idList);
        log.info("原材料补偿开始，wlIdList：【{}】，符合条件的数量：【{}】", wlIdList, syncErpWaybillList.size());

        for (SyncErpWaybill erpWaybill : syncErpWaybillList) {
            String orgState = erpWaybill.getOrgState();
            if (StringUtils.isBlank(orgState)) {
                log.info("该运单状态为空，直接默认已卸货状态，运单号：【{}】", erpWaybill.getWLID());
                erpWaybill.setOrgState(ErpOrgState.YXH);
            }
            log.info("开始补偿运单：【{}】", JsonUtils.getJson(erpWaybill));
            syncErpWaybillService.saveOrUpdate(erpWaybill);
        }
        return Json.succ();
    }
}
